package com.bearingnavi.android.overlays;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;

import com.bearingnavi.android.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class ArrowOverlay extends Overlay {
	
	private GeoPoint geoPoint;
	private Resources resources;
	private float degrees;

	public ArrowOverlay(GeoPoint geoPoint, Resources resources, float degrees) {

		this.geoPoint = geoPoint;
		this.resources = resources;
		this.degrees = degrees;
		
	}
	
	public float getDegrees() {
		return degrees;
	}
	
	public void setGeoPoint(GeoPoint geoPoint) {
		this.geoPoint = geoPoint;
	}
	public void setDegrees(float degrees) {
		this.degrees = degrees;
	}
	
	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);
		final int imageSize = 32;
		Paint paint = new Paint();
		Point point = new Point();

		Projection projection = mapv.getProjection();
		projection.toPixels(geoPoint, point);
		
		canvas.drawPoint(point.x, point.y, paint);
		
		Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.arrow);
		Matrix matrix = new Matrix();
		matrix.setRotate(degrees);
		
		double g;
        g = Math.abs(Math.sin( degrees * Math.PI / 90 )); 
        float delta = imageSize + (float) (imageSize*g*0.45);
        
		canvas.drawBitmap(Bitmap.createBitmap(bitmap, 0, 0, imageSize*2, imageSize*2, matrix, false), point.x-delta, point.y-delta, paint);
		
	}

}
